package com.onehouse.infrastructure.repository.room;

import com.onehouse.domain.checkIn.adapter.repository.ICheckInRoomPriceRepository;
import com.onehouse.domain.room.model.RoomPriceEntity;
import com.onehouse.domain.room.repository.IRoomPriceRepository;
import com.onehouse.infrastructure.dao.RoomPriceDao;
import com.onehouse.infrastructure.dao.po.RoomPrice;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Repository
public class RoomPriceRepository implements IRoomPriceRepository{
    @Resource
    private RoomPriceDao roomPriceDao;

    @Override
    public int insert(RoomPriceEntity roomPriceEntity) {
        RoomPrice roomPrice = new RoomPrice();
        roomPrice.setPriceId(roomPriceEntity.getPriceId());
        roomPrice.setRoomTypeId(roomPriceEntity.getRoomTypeId());
        roomPrice.setPrice(roomPriceEntity.getPrice());
        roomPrice.setDate(roomPriceEntity.getDate());
        roomPrice.setIsDeleted(false);

        return roomPriceDao.insert(roomPrice);
    }

    @Override
    public int insertByDateRange(RoomPriceEntity roomPriceEntity, LocalDate startTime, LocalDate endTime) {
        List<RoomPrice> roomPrices = new ArrayList<>();
        LocalDate currentDate = startTime;
        while (!currentDate.isAfter(endTime)) {
            RoomPrice roomPrice = new RoomPrice();
            roomPrice.setPriceId(roomPriceEntity.getPriceId());
            roomPrice.setRoomTypeId(roomPriceEntity.getRoomTypeId());
            roomPrice.setPrice(roomPriceEntity.getPrice());
            roomPrice.setDate(currentDate);
            roomPrices.add(roomPrice);
            currentDate = currentDate.plusDays(1);
        }
        return roomPriceDao.insertByDateRange(roomPrices);
    }

    @Override
    public List<RoomPriceEntity> selectByRoomTypeId(Long roomTypeId) {
        List<RoomPriceEntity> roomPriceEntities = new ArrayList<>();
        for (RoomPrice roomPrice : roomPriceDao.selectByRoomTypeId(roomTypeId)) {
            RoomPriceEntity roomPriceEntity = new RoomPriceEntity();
            roomPriceEntity.setPriceId(roomPrice.getPriceId());
            roomPriceEntity.setRoomTypeId(roomPrice.getRoomTypeId());
            roomPriceEntity.setPrice(roomPrice.getPrice());
            roomPriceEntity.setDate(roomPrice.getDate());
            roomPriceEntities.add(roomPriceEntity);
        }
        return roomPriceEntities;
    }

    @Override
    public Optional<RoomPriceEntity> selectByTypeAndDate(RoomPriceEntity roomPriceEntity) {
        RoomPrice roomPrice = new RoomPrice();
        roomPrice.setRoomTypeId(roomPriceEntity.getRoomTypeId());
        roomPrice.setDate(roomPriceEntity.getDate());
        
        RoomPrice result = roomPriceDao.selectByTypeAndDate(roomPrice);
        if (result == null) {
            return Optional.empty();
        }
        
        RoomPriceEntity resultEntity = new RoomPriceEntity();
        resultEntity.setPriceId(result.getPriceId());
        resultEntity.setRoomTypeId(result.getRoomTypeId());
        resultEntity.setPrice(result.getPrice());
        resultEntity.setDate(result.getDate());
        
        return Optional.of(resultEntity);
    }

    @Override
    public List<RoomPriceEntity> selectList(LocalDate startTime, LocalDate endTime) {
        List<RoomPriceEntity> roomPriceEntities = new ArrayList<>();
        for (RoomPrice roomPrice : roomPriceDao.selectList(startTime, endTime)) {
            RoomPriceEntity roomPriceEntity = new RoomPriceEntity();
            roomPriceEntity.setPriceId(roomPrice.getPriceId());
            roomPriceEntity.setRoomTypeId(roomPrice.getRoomTypeId());
            roomPriceEntity.setPrice(roomPrice.getPrice());
            roomPriceEntity.setDate(roomPrice.getDate());
            roomPriceEntities.add(roomPriceEntity);
        }
        return roomPriceEntities;
    }

    @Override
    public int updateByDate(RoomPriceEntity roomPriceEntity) {
        RoomPrice roomPrice = new RoomPrice();
        roomPrice.setPriceId(roomPriceEntity.getPriceId());
        roomPrice.setRoomTypeId(roomPriceEntity.getRoomTypeId());
        roomPrice.setPrice(roomPriceEntity.getPrice());
        roomPrice.setDate(roomPriceEntity.getDate());

        return roomPriceDao.updateByDate(roomPrice);
    }

    @Override
    public int updateByDateRange(RoomPriceEntity roomPriceEntity, LocalDate startTime, LocalDate endTime) {
        RoomPrice roomPrice = new RoomPrice();
        roomPrice.setPriceId(roomPriceEntity.getPriceId());
        roomPrice.setRoomTypeId(roomPriceEntity.getRoomTypeId());
        roomPrice.setPrice(roomPriceEntity.getPrice());
        roomPrice.setDate(roomPriceEntity.getDate());

        return roomPriceDao.updateByDateRange(roomPrice, startTime, endTime);
    }


    @Override
    public int deleteById(Long priceId) {
        return roomPriceDao.deleteById(priceId);
    }
}